package com.rover.crm.controller;

import com.grid.DataGrid;
import com.grid.Page;
import com.rover.system.controller.BaseController;
import com.rover.crm.entity.BillingRecords;
import com.rover.crm.service.BillingRecordsService;
import com.utils.JsonUtil;
import com.utils.HqlGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 *开票记录 Controller
 */
@Controller
@RequestMapping("/billingRecordsController")
public class BillingRecordsController  extends BaseController {

    @Autowired
    private BillingRecordsService service;


    // 用户管理
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, Page page,
                                     HttpServletResponse response) {
        Map<String, Object> map =null;
        try {
            HqlGenerateUtil.installHql(request, BillingRecords.class, page);
            DataGrid dataGrid = service.query(page);
            map = JsonUtil.JSONString(dataGrid.getResult(),
                    dataGrid.getTotalCount());
        } catch (Exception e) {
            map = new HashMap<String, Object>();
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(BillingRecords billingRecords, HttpServletRequest request)
             {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
            service.saveOrUpdate(billingRecords);
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> delete(@RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        // String id = request.getParameter("id");
        try {
            service.delete(BillingRecords.class, Integer.valueOf(id));
            map.put("success", true);
            map.put("message", "");

        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }



}
